package n5;

/**
 * @author 苏文广
 * @since 2023/4/13
 */
public class Solution {

    public static void main(String[] args) {
        System.out.println(new Solution().longestPalindrome("aacabdkacaa"));
    }

    public String longestPalindrome(String s) {
        char[] chars = s.toCharArray();

        int max = 0;
        int len = chars.length;
        int start = 0;
        int end = 0;

        for (int i = 0; i < len; i++){
            for (int j = i + 1; j < len; j++){
                if(check(chars,i,j) && (j - i) > max){
                    max = j - i;
                    start = i;
                    end = j;
                }
            }
        }
        StringBuilder stringBuilder = new StringBuilder();
        for (int i = start;i<=end;i++){
            stringBuilder.append(chars[i]);
        }
        return stringBuilder.toString();
    }

    public boolean check(char[] chars, int i ,int j){
        int mid = (j - i) / 2 + i;
        int n = j;
        for (int m = i; m <= mid; m++ ){
            if (chars[m] != chars[n]){
                return false;
            }
            n--;
        }
        return true;
    }
}
